package com.xinfu.newproject.util;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.xinfu.newproject.R;

public class LoginUtils {
    private Context mContext;
    boolean bool1 = false;
    boolean bool2 = false;

    public LoginUtils(Context context) {
        mContext = context;
    }

    public void setPasswordListener(final EditText edit, final TextView hint, final View view, final ImageView look, final EditPhoneListener listener) {
        edit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                String editText = edit.getText().toString().trim();
                if (WHelperUtil.isPWDMatch(mContext, editText) && editText.length() >= 6) {
                    bool1 = true;
                } else {
                    bool1 = false;
                }
                if (hasFocus && editText.length() == 0) {
                    view.setBackgroundColor(0xffffe313);
                    look.setVisibility(View.GONE);

                } else if (!hasFocus) {
                    if (editText.equals("")) {
                        hint.setVisibility(View.INVISIBLE);
                        view.setBackgroundColor(0xffd3d3d3);
                        look.setVisibility(View.GONE);
                        return;
                    }
                    if (bool1) {
                        look.setVisibility(View.VISIBLE);
                        listener.isPhoneRight(true);
                        hint.setText("格式正确");
                        hint.setTextColor(0xff3CD21E);
                        hint.setVisibility(View.VISIBLE);
                        view.setBackgroundColor(0xff3CD21E);
                    } else {
                        look.setVisibility(View.VISIBLE);
                        listener.isPhoneRight(false);
                        hint.setText("格式仅限字母、数字或下划线");
                        hint.setTextColor(0xffe40b12);
                        hint.setVisibility(View.VISIBLE);
                        view.setBackgroundColor(0xffe40b12);

                    }
                    if (editText.length() < 6) {
                        look.setVisibility(View.VISIBLE);
                        listener.isPhoneRight(false);
                        hint.setText("密码长度最少6位");
                        hint.setTextColor(0xffe40b12);
                        hint.setVisibility(View.VISIBLE);
                        view.setBackgroundColor(0xffe40b12);

                    }
                }
            }
        });
        edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String editText = s.toString().trim();
                if (WHelperUtil.isPWDMatch(mContext, editText) && editText.length() >= 6) {
                    bool1 = true;
                } else {
                    bool1 = false;
                }
                if (editText.equals("")) {
                    hint.setVisibility(View.INVISIBLE);
                    look.setVisibility(View.GONE);
                    view.setBackgroundColor(0xffd3d3d3);
                    return;
                }
                if (bool1) {
                    look.setVisibility(View.VISIBLE);
                    listener.isPhoneRight(true);
                    hint.setText("格式正确");
                    hint.setTextColor(0xff3CD21E);
                    hint.setVisibility(View.VISIBLE);
                    view.setBackgroundColor(0xff3CD21E);
                } else {
                    look.setVisibility(View.VISIBLE);

                    listener.isPhoneRight(false);
                    hint.setText("格式仅限字母、数字或下划线");
                    hint.setTextColor(0xffe40b12);
                    hint.setVisibility(View.VISIBLE);
                    view.setBackgroundColor(0xffe40b12);

                }
                if (editText.length() > 20) {
                    look.setVisibility(View.VISIBLE);

                    /**
                     * 截取最大长度
                     */
                    int selectionStart = edit.getSelectionStart();
                    int selectionEnd = edit.getSelectionEnd();
                    s.delete(selectionStart - 1, selectionEnd);
                    edit.setText(s.toString());
                    listener.isPhoneRight(false);
                    hint.setText("超出密码最大长度20位");
                    edit.setSelection(s.length());

                    hint.setTextColor(0xffe40b12);
                    hint.setVisibility(View.VISIBLE);
                    view.setBackgroundColor(0xffe40b12);

                }
                if (editText.length() < 6) {
                    listener.isPhoneRight(false);
                    hint.setVisibility(View.INVISIBLE);
                    view.setBackgroundColor(0xffffe313);
                }

            }
        });
    }

    public void setPhoneListener(final EditText edit, final TextView hint, final ImageView phoneCancle, final View view, final EditPasswordListener listener) {
        edit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                String editText = edit.getText().toString().trim();
                if (hasFocus && editText.length() == 0) {
                    view.setBackgroundColor(0xffffe313);
                } else if (!hasFocus) {
                    if (editText.equals("")) {
                        hint.setVisibility(View.INVISIBLE);
                        view.setBackgroundColor(0xffd3d3d3);
                        return;
                    }
                    if (editText.equals("")) {
                        phoneCancle.setVisibility(View.GONE);
                    } else {
                        phoneCancle.setVisibility(View.VISIBLE);
                    }

                    if (WHelperUtil.isMobileRight(mContext, editText)) {
                        bool2 = true;
                    } else {
                        bool2 = false;
                    }
                    if (bool2) {
                        listener.isPasswordRight(true);
                        hint.setText("格式正确");
                        hint.setTextColor(0xff3CD21E);
                        hint.setVisibility(View.VISIBLE);
                        view.setBackgroundColor(0xff3CD21E);
                    } else if (editText.length() < 11 || !bool2) {
                        listener.isPasswordRight(false);
                        hint.setText("请输入正确的手机号码");
                        hint.setTextColor(0xffe40b12);
                        hint.setVisibility(View.VISIBLE);
                        view.setBackgroundColor(0xffe40b12);

                    }
                }
            }
        });
        edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String editText = s.toString().trim();
                if (editText.equals("")) {
                    phoneCancle.setVisibility(View.GONE);
                } else {
                    phoneCancle.setVisibility(View.VISIBLE);
                }

                if (WHelperUtil.isMobileRight(mContext, editText)) {
                    bool2 = true;
                } else {
                    bool2 = false;
                }
                if (editText.equals("")) {
                    hint.setVisibility(View.INVISIBLE);
                    view.setBackgroundColor(0xffd3d3d3);
                    return;
                }
                if (bool2) {
                    listener.isPasswordRight(true);
                    hint.setText("格式正确");
                    hint.setTextColor(0xff3CD21E);
                    hint.setVisibility(View.VISIBLE);
                    view.setBackgroundColor(0xff3CD21E);
                } else if (!bool2 && editText.length() > 11) {
                    listener.isPasswordRight(false);
                    /**
                     * 截取最大长度
                     */
                    int selectionStart = edit.getSelectionStart();
                    int selectionEnd = edit.getSelectionEnd();
                    s.delete(selectionStart - 1, selectionEnd);
                    edit.setText(s.toString());

                    hint.setText("请输入正确的手机号码");
                    edit.setSelection(s.length());

                    hint.setTextColor(0xffe40b12);
                    hint.setVisibility(View.VISIBLE);
                    view.setBackgroundColor(0xffe40b12);

                } else if (editText.length() < 11) {
                    listener.isPasswordRight(false);
                    hint.setVisibility(View.INVISIBLE);
                    view.setBackgroundColor(0xffffe313);
                } else if (!bool2) {
                    listener.isPasswordRight(false);
                    hint.setText("请输入正确的手机号码");
                    hint.setTextColor(0xffe40b12);
                    hint.setVisibility(View.VISIBLE);
                    view.setBackgroundColor(0xffe40b12);
                }
            }
        });
    }

    /**
     * 判断手机是否格式正确
     *
     * @param
     * @param edit
     * @param hint
     * @param view
     * @param listener
     */
    public void isPhoneListener(final EditText edit, final TextView hint, final ImageView phoneCancle, final View view, final EditIsPhoneListener listener) {
        edit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                String editText = edit.getText().toString().trim();
                if (hasFocus && editText.length() == 0) {
//                    view.setBackgroundColor(0xffffe313);
                } else if (!hasFocus) {
                    if (editText.equals("")) {
                        hint.setVisibility(View.INVISIBLE);
//                        view.setBackgroundColor(0xffd3d3d3);
                        return;
                    }
//                    if (editText.equals("")) {
//                        phoneCancle.setImageResource(R.mipmap.question_mark_icon);
//                    } else {
//                        phoneCancle.setImageResource(R.mipmap.question_mark_icon);
//                    }

                    if (WHelperUtil.isMobileRight(mContext, editText)) {
                        bool2 = true;
                    } else {
                        bool2 = false;
                    }
                    if (bool2) {
                        listener.isPhoneListenerRight(true);
                        hint.setText("格式正确");
                        hint.setTextColor(0xff3CD21E);
                        hint.setVisibility(View.VISIBLE);
//                        view.setBackgroundColor(0xff3CD21E);
                        phoneCancle.setImageResource(R.mipmap.input_ok_icon);
                    } else if (editText.length() < 11 || !bool2) {
                        listener.isPhoneListenerRight(false);
                        hint.setText("请输入正确的手机号码");
                        hint.setTextColor(0xffe40b12);
                        hint.setVisibility(View.VISIBLE);
//                        view.setBackgroundColor(0xffe40b12);
                        phoneCancle.setImageResource(R.mipmap.question_mark_icon);
                    }
                }
            }
        });
        edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String editText = s.toString().trim();
//                if (editText.equals("")) {
//                    phoneCancle.setVisibility(View.GONE);
//                } else {
//                    phoneCancle.setVisibility(View.VISIBLE);
//                }

                if (WHelperUtil.isMobileRight(mContext, editText)) {
                    bool2 = true;
                } else {
                    bool2 = false;
                }
                if (editText.equals("")) {
                    hint.setVisibility(View.INVISIBLE);
//                    view.setBackgroundColor(0xffd3d3d3);
                    return;
                }
                if (bool2) {
                    listener.isPhoneListenerRight(true);
                    hint.setText("格式正确");
                    hint.setTextColor(0xff3CD21E);
                    hint.setVisibility(View.VISIBLE);
//                    view.setBackgroundColor(0xff3CD21E);
                    phoneCancle.setImageResource(R.mipmap.input_ok_icon);
                } else if (!bool2 && editText.length() > 11) {
                    listener.isPhoneListenerRight(false);
                    /**
                     * 截取最大长度
                     */
                    int selectionStart = edit.getSelectionStart();
                    int selectionEnd = edit.getSelectionEnd();
                    s.delete(selectionStart - 1, selectionEnd);
                    edit.setText(s.toString());

                    hint.setText("请输入正确的手机号码");
                    edit.setSelection(s.length());

                    hint.setTextColor(0xffe40b12);
                    hint.setVisibility(View.VISIBLE);
//                    view.setBackgroundColor(0xffe40b12);
                    phoneCancle.setImageResource(R.mipmap.question_mark_icon);
                } else if (editText.length() < 11) {
                    listener.isPhoneListenerRight(false);
                    hint.setVisibility(View.INVISIBLE);
//                    view.setBackgroundColor(0xffffe313);
                    phoneCancle.setImageResource(R.mipmap.question_mark_icon);
                } else if (!bool2) {
                    listener.isPhoneListenerRight(false);
                    hint.setText("请输入正确的手机号码");
                    hint.setTextColor(0xffe40b12);
                    hint.setVisibility(View.VISIBLE);
//                    view.setBackgroundColor(0xffe40b12);
                    phoneCancle.setImageResource(R.mipmap.question_mark_icon);
                }
            }
        });
    }


    public void setAuthCodeListener(final EditText edit, final TextView hint, final View view, final ImageView codeCanle, final EditAuthCodeListener listener) {
        edit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                String editText = edit.getText().toString().trim();
                if (hasFocus && editText.length() == 0) {
//                    view.setBackgroundColor(0xffffe313);
                } else if (!hasFocus) {
                    if (editText.equals("")) {
                        codeCanle.setVisibility(View.GONE);
                    } else {
                        codeCanle.setVisibility(View.VISIBLE);
                    }
                    if (editText.equals("")) {
                        hint.setVisibility(View.INVISIBLE);
                        view.setBackgroundColor(0xffd3d3d3);
                        return;
                    }

                    if (editText.length() == 6) {
                        listener.isAuthCodeRight(true);
                        hint.setText("格式正确");
                        hint.setTextColor(0xff3CD21E);
                        hint.setVisibility(View.VISIBLE);
                        view.setBackgroundColor(0xff3CD21E);
                    } else if (editText.length() < 4 || editText.length() > 4) {
                        listener.isAuthCodeRight(false);
                        hint.setText("请输入4位验证码");
                        hint.setTextColor(0xffe40b12);
                        hint.setVisibility(View.VISIBLE);
                        view.setBackgroundColor(0xffe40b12);
                    }
                }
            }
        });
        edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String editText = s.toString().trim();
                boolean bool = false;
                if (editText.length() == 6) {
                    bool = true;
                } else {
                    bool = false;
                }
                if (editText.equals("")) {
                    codeCanle.setVisibility(View.GONE);
                } else {
                    codeCanle.setVisibility(View.VISIBLE);
                }
                if (editText.equals("")) {
                    hint.setVisibility(View.INVISIBLE);
                    view.setBackgroundColor(0xffffe313);
                    return;
                }

                if (bool) {
                    listener.isAuthCodeRight(true);
                    hint.setText("格式正确");
                    hint.setTextColor(0xff3CD21E);
                    hint.setVisibility(View.VISIBLE);
                    view.setBackgroundColor(0xff3CD21E);
                } else if (editText.length() > 6) {
                    /**
                     * 截取最大长度
                     */
                    int selectionStart = edit.getSelectionStart();
                    int selectionEnd = edit.getSelectionEnd();
                    s.delete(selectionStart - 1, selectionEnd);
                    edit.setText(s.toString());
                    edit.setSelection(s.length());

                    listener.isAuthCodeRight(true);
                    hint.setVisibility(View.INVISIBLE);
                    view.setBackgroundColor(0xffffe313);

                }

            }
        });
    }

    public void setAuthPicCodeListener(final EditText edit, final TextView hint, final View view,
                                       final ImageView codeCanle, final EditAuthPicCodeListener listener) {
        edit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                String editText = edit.getText().toString().trim();
                if (hasFocus && editText.length() == 0) {
//                    view.setBackgroundColor(0xffffe313);
                } else if (!hasFocus) {
                    if (editText.equals("")) {
                        codeCanle.setVisibility(View.GONE);
                    } else {
                        codeCanle.setVisibility(View.GONE);
                    }
                    if (editText.equals("")) {
                        hint.setVisibility(View.INVISIBLE);
                        view.setBackgroundColor(0xffd3d3d3);
                        return;
                    }

                    if (editText.length() == 5) {
                        listener.isAuthPicCodeRight(true);
                        hint.setText("格式正确");
                        hint.setTextColor(0xff3CD21E);
                        hint.setVisibility(View.VISIBLE);
                        view.setBackgroundColor(0xff3CD21E);
                    } else if (editText.length() < 5 || editText.length() > 5) {
                        listener.isAuthPicCodeRight(false);
                        hint.setText("请输入5位验证码");
                        hint.setTextColor(0xffe40b12);
                        hint.setVisibility(View.VISIBLE);
                        view.setBackgroundColor(0xffe40b12);
                    }
                }
            }
        });
        edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String editText = s.toString().trim();
                boolean bool = false;
                if (editText.length() == 5) {
                    bool = true;
                } else {
                    bool = false;
                }
                if (editText.equals("")) {
                    codeCanle.setVisibility(View.GONE);
                } else {
                    codeCanle.setVisibility(View.GONE);
                }
                if (editText.equals("")) {
                    hint.setVisibility(View.INVISIBLE);
                    view.setBackgroundColor(0xffffe313);
                    return;
                }

                if (bool) {
                    listener.isAuthPicCodeRight(true);
                    hint.setText("格式正确");
                    hint.setTextColor(0xff3CD21E);
                    hint.setVisibility(View.VISIBLE);
                    view.setBackgroundColor(0xff3CD21E);
                } else if (editText.length() > 5) {
                    /**
                     * 截取最大长度
                     */
                    int selectionStart = edit.getSelectionStart();
                    int selectionEnd = edit.getSelectionEnd();
                    s.delete(selectionStart - 1, selectionEnd);
                    edit.setText(s.toString());
                    //光标移到最后
                    edit.setSelection(s.length());
                    listener.isAuthPicCodeRight(true);
                    hint.setVisibility(View.INVISIBLE);
                    view.setBackgroundColor(0xffffe313);

                }

            }
        });
    }

    public interface EditPhoneListener {
        void isPhoneRight(boolean bool);
    }

    public interface EditPasswordListener {
        void isPasswordRight(boolean bool);
    }

    public interface EditAuthCodeListener {
        void isAuthCodeRight(boolean bool);
    }

    public interface EditAuthPicCodeListener {
        void isAuthPicCodeRight(boolean bool);
    }

    public interface EditIsPhoneListener {
        void isPhoneListenerRight(boolean bool);
    }
}
